1 using System;
2 using
System.Collections.Generic;
3 using
System.IO;
4 using
System.Linq;
5 using
System.Text;
6 using
System.Xml;
7 using
System.Xml.Linq;
8
9 using
UnityEditor;
10 using
UnityEngine;
11
12
13 namespace
Tiled2Unity
14 {
15     
// Concentrates on the Xml file being imported
16     
partial class ImportTiled2Unity
17     {
18         
public static readonly string ThisVersion = "0.9.4.5";
19
20         
public void XmlImported(string xmlPath)
21         {
22             XDocument xml = XDocument.Load(xmlPath);
23
24             CheckVersion(xmlPath, xml);
25
26             
// Import asset files.
27             
// (Note that textures should be imported before meshes)
28             ImportTexturesFromXml(xml);
29             CreateMaterialsFromInternalTextures(xml);
30             ImportMeshesFromXml(xml);
31         }
32
33         
private void CheckVersion(string xmlPath, XDocument xml)
34         {
35             
string version = xml.Root.Attribute("version").Value;
36             
if (version != ThisVersion)
37             {
38                 Debug.LogWarning(
string.Format("Imported Tiled2Unity file '{0}' was exported with version {1}. We are expecting version {2}", xmlPath, version, ThisVersion));
39             }
40         }
41
42         
private void ImportTexturesFromXml(XDocument xml)
43         {
44             
var texData = xml.Root.Elements("ImportTexture");
45             
foreach (var tex in texData)
46             {
47                 
string name = tex.Attribute("filename").Value;
48                 
string data = tex.Value;
49
50                 
// The data is gzip compressed base64 string. We need the raw bytes.
51                 
//byte[] bytes = ImportUtils.GzipBase64ToBytes(data);
52                 
byte[] bytes = ImportUtils.Base64ToBytes(data);
53
54                 
// Save and import the texture asset
55                 {
56                     
string pathToSave = ImportUtils.GetTexturePath(name);
57                     ImportUtils.ReadyToWrite(pathToSave);
58                     File.WriteAllBytes(pathToSave, bytes);
59                     AssetDatabase.ImportAsset(pathToSave, ImportAssetOptions.ForceSynchronousImport);
60                 }
61
62                 
// Create a material if needed in prepartion for the texture being successfully imported
63                 {
64                     
string materialPath = ImportUtils.GetMaterialPath(name);
65                     Material material = AssetDatabase.LoadAssetAtPath(materialPath,
typeof(Material)) as Material;
66                     
if (material == null)
67                     {
68                         
// We need to create the material afterall
69                         
// Use our custom shader
70                         material =
new Material(Shader.Find("Tiled/TextureTintSnap"));
71                         ImportUtils.ReadyToWrite(materialPath);
72                         AssetDatabase.CreateAsset(material, materialPath);
73                     }
74                 }
75             }
76         }
77
78         
private void CreateMaterialsFromInternalTextures(XDocument xml)
79         {
80             
var texData = xml.Root.Elements("InternalTexture");
81             
foreach (var tex in texData)
82             {
83                 
string texAssetPath = tex.Attribute("assetPath").Value;
84                 
string materialPath = ImportUtils.GetMaterialPath(texAssetPath);
85
86                 Material material = AssetDatabase.LoadAssetAtPath(materialPath,
typeof(Material)) as Material;
87                 
if (material == null)
88                 {
89                     
// Create our material
90                     material =
new Material(Shader.Find("Tiled/TextureTintSnap"));
91
92                     
// Assign to it the texture that is already internal to our Unity project
93                     Texture2D texture2d = AssetDatabase.LoadAssetAtPath(texAssetPath,
typeof(Texture2D)) as Texture2D;
94                     material.SetTexture(
"_MainTex", texture2d);
95
96                     
// Write the material to our asset database
97                     ImportUtils.ReadyToWrite(materialPath);
98                     AssetDatabase.CreateAsset(material, materialPath);
99                 }
100             }
101         }
102
103         
private void ImportMeshesFromXml(XDocument xml)
104         {
105             
var meshData = xml.Root.Elements("ImportMesh");
106             
foreach (var mesh in meshData)
107             {
108                 
// We're going to create/write a file that contains our mesh data as a Wavefront Obj file
109                 
// The actual mesh will be imported from this Obj file
110
111                 
string name = mesh.Attribute("filename").Value;
112                 
string data = mesh.Value;
113
114                 
// The data is in base64 format. We need it as a raw string.
115                 
string raw = ImportUtils.Base64ToString(data);
116
117                 
// Save and import the asset
118                 
string pathToMesh = "Assets/Tiled2Unity/Meshes/" + name;
119                 ImportUtils.ReadyToWrite(pathToMesh);
120                 File.WriteAllText(pathToMesh, raw, Encoding.UTF8);
121                 AssetDatabase.ImportAsset(pathToMesh, ImportAssetOptions.ForceSynchronousImport);
122             }
123         }
124     }
125 }


Concentrates on the Xml file being imported

Import asset files.

(Note that textures should be imported before meshes)

The data is gzip compressed base64 string. We need the raw bytes.

byte[] bytes = ImportUtils.GzipBase64ToBytes(data);

Save and import the texture asset

Create a material if needed in prepartion for the texture being successfully imported

We need to create the material afterall

Use our custom shader

Create our material

Assign to it the texture that is already internal to our Unity project

Write the material to our asset database

We're going to createwrite a file that contains our mesh data as a Wavefront Obj file

The actual mesh will be imported from this Obj file

The data is in base64 format. We need it as a raw string.

Save and import the asset




Trò chơi đua xe động vật trong UNITY Engine 114.912 lượt xem

Gõ tìm kiếm nhanh...